package com.echoesnet.echoesshortvideo.Activities;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.view.KeyEvent;
import android.view.View;

import com.echoesnet.echoesshortvideo.R;
import com.echoesnet.echoesshortvideo.views.dialog.CustomAlertDialog;
import com.orhanobut.logger.Logger;

public class WelcomeAct extends Activity
{
    private final static String TAG=WelcomeAct.class.getSimpleName();
    private Activity mAct;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_welcome);
        mAct = this;
        startApp();
    }

    private void startApp()
    {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
        {
            if (ContextCompat.checkSelfPermission(mAct, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                            != PackageManager.PERMISSION_GRANTED)
            {
                //getWindow().setBackgroundDrawableResource(R.drawable.default_start);
                ActivityCompat.requestPermissions(mAct,
                        new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 3);
            }
            else
            {
                startMainAct();
            }
        }
        else
        {
            startMainAct();
        }
    }

    //权限回调
    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults)
    {
        Logger.t(TAG).d("权限回调 " + requestCode);
        switch (requestCode)
        {
            case 3:
                boolean isGranted = true;
                for (int grantResult : grantResults)
                {
                    if (grantResult != PackageManager.PERMISSION_GRANTED)
                    {
                        isGranted = false;
                        break;
                    }
                }
                if (isGranted)
                {
                   startMainAct();
                }
                else
                {
                    new CustomAlertDialog(WelcomeAct.this)
                            .builder()
                            .setMsg("应用没有获取必须的权限无法使用,请到设置页面打开相应的权限")
                            .setCancelable(false)
                            .setPositiveButton("确认", new View.OnClickListener()
                            {
                                @Override
                                public void onClick(View v)
                                {
                                    finish();
                                }
                            }).show();
                }
                break;
        }
    }
    private void startMainAct()
    {
        new Handler().postDelayed(() -> {
            startActivity(new Intent(mAct, MainAct.class));
            finish();
        },  1000);
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        if ((keyCode == KeyEvent.KEYCODE_BACK))
        {
            return false;
        }
        else
        {
            return super.onKeyDown(keyCode, event);
        }
    }
}
